package de.dhbw.karlsruhe.picsim.model.decode;

import de.dhbw.karlsruhe.picsim.model.commands.PicInstruction;
import de.dhbw.karlsruhe.picsim.model.commands.impl.*;
import de.dhbw.karlsruhe.picsim.model.memory.OpCode;
import de.dhbw.karlsruhe.picsim.util.Bits;
import de.dhbw.karlsruhe.picsim.util.impl.BitUtilities;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NotNull;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: Apr 18, 2010
 * Time: 1:06:11 PM
 */
final class LiteralDecoder11 implements Decoder {
    private static final Log log = LogFactory.getLog(LiteralDecoder11.class);

    @NotNull
    @Override
    public PicInstruction decode(@NotNull final OpCode opCode) {
        log.debug("now decoding opcode" + opCode);


        final Bits bits = opCode.getValue();
        final Bits argument = BitUtilities.subBits(bits, 0, 8);
        if (!bits.isSet(11) && !bits.isSet(10)) {
            return new MoveLiteralToW(argument);
        } else if (bits.isSet(11) && bits.isSet(10) && bits.isSet(9)) {
            return new AddLW(argument);
        } else if (bits.isSet(11) && !bits.isSet(10) && !bits.isSet(9) && bits.isSet(8)) {
            return new AndLW(argument);
        } else if (bits.isSet(11) && !bits.isSet(10) && !bits.isSet(9) && !bits.isSet(8)) {
            return new IORLW(argument);
        } else if (!bits.isSet(11) && bits.isSet(10)) {
            return new ReturnWithLiteralInW(argument);
        } else if (bits.isSet(11) && bits.isSet(10) && !bits.isSet(9)) {
            return new SUBLW(argument);
        } else if (bits.isSet(11) && !bits.isSet(10) && bits.isSet(9) && !bits.isSet(8)) {
            return new XORLW(argument);
        }


        throw new UnsupportedOperationException();
    }
}